package cn.vinann.demo.config.dataSource;

import java.util.HashMap;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

@Configuration
@DependsOn("transactionManager")
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySecondary",
        transactionManagerRef="transactionManager",
        basePackages= { "cn.vinann.demo.repository.akuc" }) //设置Repository所在位置
public class SecondaryConfig {

    @Autowired 
    @Qualifier("secondaryDataSource")
    private DataSource dataSource;
    
    @Autowired
	private JpaVendorAdapter jpaVendorAdapter;

    @Bean(name = "entityManagerFactorySecondary")
    @DependsOn("transactionManager")
    public LocalContainerEntityManagerFactoryBean accountEntityManager() throws Throwable {

		HashMap<String, Object> properties = new HashMap<String, Object>();
		properties.put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
		properties.put("javax.persistence.transactionType", "JTA");

		LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
		entityManager.setJtaDataSource(dataSource);
		entityManager.setJpaVendorAdapter(jpaVendorAdapter);
		entityManager.setPackagesToScan("cn.vinann.demo.domain.akuc");
		entityManager.setPersistenceUnitName("akucPersistenceUnit");
		entityManager.setJpaPropertyMap(properties);
		return entityManager;
	}
}